package com.example.application.views.change;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.security.PermitAll;

import com.example.application.compose.BadgeBasic;
import com.example.application.compose.BadgeIcons;
import com.example.application.compose.NumericField;
import com.example.application.views.MainLayout;
import com.example.application.data.entity.Book;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.dom.ThemeList;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.theme.lumo.Lumo;

@Route(value = "copy", layout = MainLayout.class)
@PageTitle("测试复制粘贴功能")
@PermitAll
public class CopyView extends VerticalLayout {
    private List<String> items = new ArrayList<>(
            Arrays.asList("Chrome", "Edge", "Firefox", "Safari"));
            
    public CopyView() {

        Div layout = new Div();
        add(layout);
        
        BadgeBasic bd=new BadgeBasic();
        add(bd);
        BadgeIcons bdic=new BadgeIcons();
        add(bdic);
        NumericField num=new NumericField(10,5,-10);
        add(num);
        
        TextField textField = new TextField();
        textField.setLabel("Street Address");
        textField.setValue("清华大学");
        // textField.setClearButtonVisible(true);
        textField.setPrefixComponent(VaadinIcon.ARROWS.create());
        add(textField);
        H1 h1 = new H1("你好,Darkness 暗黑模式。");
        Button toggleButton = new Button("Toggle dark theme", click -> {
            ThemeList themeList = UI.getCurrent().getElement().getThemeList(); 

            if (themeList.contains(Lumo.DARK)) { 
                themeList.remove(Lumo.DARK);
            } else {
                themeList.add(Lumo.DARK);
            }
        });
        ComboBox<String> comboBox = new ComboBox<>("DIY");
        comboBox.setAllowCustomValue(true);
        comboBox.addCustomValueSetListener(e -> {
            String customValue = e.getDetail();
            items.add(customValue);
            comboBox.setItems(items);
            // comboBox.setValue(customValue);
        });
        comboBox.setItems(items);
        comboBox.setHelperText("选择或者输入一个浏览器名称！");
        add(comboBox);
        toggleButton.getElement().setAttribute("theme", Lumo.DARK);
        add(toggleButton, h1);

                Binder<Book> binder = new Binder<>(Book.class);

        TextField titleField = new TextField();
        titleField.setLabel("HI");

        // Start by defining the Field instance to use
        binder.forField(titleField)
                // Finalize by doing the actual binding
                // to the Person class
                .bind(
                        // Callback that loads the title
                        // from a person instance
                        Book::getSrc_str,
                        // Callback that saves the title
                        // in a person instance
                        Book::setSrc_str);

        add(titleField);

        // Shorthand for cases without extra configuration
        // binder.bind(nameField, Book::getSrc_str,
        // Book::getDeal_str);
    }

    protected void onAttach(AttachEvent attachEvent) {
        UI ui = getUI().get();
        Button button = new Button("Logout", event -> {
            // Redirect this page immediately
            ui.getPage().executeJs("window.location.href='logout.html'");

            // Close the session
            ui.getSession().close();
        });

        add(button);

        // Notice quickly if other UIs are closed
        ui.setPollInterval(3000);
    }
    
}
